package com.xnyzc.lhy.order.controller.capi.user.address;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.order.entity.param.user.address.UpdateAddressCollectParam;
import com.xnyzc.lhy.order.service.user.address.IOaCAddressCollectService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @Description 收藏地址
 * @Author zhuyubin
 * @Date 2019-09-18 10:22
 **/
@RestController
@RequestMapping(value = "/capi/addressCollect")
public class AddressCollectController {

    @Autowired
    private IOaCAddressCollectService iOaCAddressCollectService;

    /**
     * 删除地点
     * @param updateAddressCollectParam 入参实体
     * @return
     */
    @PostMapping(value = "/deleteAddressCollect")
    public Rv deleteAddressCollect(@ApiParam(name = "updateAddressCollectParam", value = "入参实体") @RequestBody UpdateAddressCollectParam updateAddressCollectParam){
        Long userId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        Integer type = updateAddressCollectParam.getType();
        if (CheckUtil.objIsEmpty(type)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return Rv.wrap(EErrorCode.success, iOaCAddressCollectService.deleteAddressCollect(userId, tag, updateAddressCollectParam), new HashMap<>(16));
    }

    /**
     * 查询收藏地址*
     * @return
     */
    @GetMapping(value = "/getAddressCollect")
    public Rv getAddressCollect(){
        Long userId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return Rv.wrap(iOaCAddressCollectService.getAddressCollect(userId, tag));
    }

    /**
     * 更新地点收藏*
     * @param updateAddressCollectParam 入参实体
     * @return
     */
    @PostMapping(value = "/updateAddressCollect")
    public Rv updateAddressCollect(@ApiParam(name = "updateAddressCollectParam", value = "入参实体") @RequestBody UpdateAddressCollectParam updateAddressCollectParam){
        Long userId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        String address = updateAddressCollectParam.getAddress();
        String lng = updateAddressCollectParam.getLng();
        String lat = updateAddressCollectParam.getLat();
        Integer type = updateAddressCollectParam.getType();
        if (CheckUtil.objIsEmpty(address, lng, lat, type)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return Rv.wrap(EErrorCode.success, iOaCAddressCollectService.updateAddressCollect(userId, tag, updateAddressCollectParam), new HashMap<>(16));
    }
}
